//
//  TBStatusCell.swift
//  新浪微博
//
//  Created by bin on 16/1/29.
//  Copyright © 2016年 XMG. All rights reserved.
//

import UIKit

class TBStatusCell: UITableViewCell {

    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var vipImageView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var verifyImageView: UIImageView!
    @IBOutlet weak var contentLabel: UILabel!
    @IBOutlet weak var picCollectionView: UICollectionView!
    @IBOutlet weak var collecHCons: NSLayoutConstraint!
    @IBOutlet weak var collecWCons: NSLayoutConstraint!
    @IBOutlet weak var BottomView: UIView!
    @IBOutlet weak var originContentLabel: UILabel!
    @IBOutlet weak var ContentLabelWCons: NSLayoutConstraint!
    
    //数据模型
    var status:TBStatusViewModel?{
        didSet{
            
            let user = status?.status.user
            iconImageView.sd_setImageWithURL(user?.avatarURL)
            nameLabel.text = user?.screen_name
            vipImageView.image = user?.mbrankImage
            timeLabel.text = status?.createdTime
            sourceLabel.text = status?.sourceText
            verifyImageView.image = user?.verifiedImage
            contentLabel.text = status?.status.text
            collecHCons.constant = (status?.rect.colleRect.height)!
            collecWCons.constant = (status?.rect.colleRect.width)!
            let layout = picCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
            if status?.rect.imageRect != CGSizeZero
                {
                    layout.itemSize = (status?.rect.imageRect)!
                }
            if status?.status.retweeted_status != nil{
                originContentLabel.text = status?.status.retweeted_status?.text
            }

            picCollectionView.reloadData()
        }
    }
    
    func heightForCell() -> CGFloat{
        layoutIfNeeded()
        return CGRectGetMaxY(BottomView.frame)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        ContentLabelWCons.constant = ScreenW - 2 * 10
    }
}


extension TBStatusCell: UICollectionViewDataSource{
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        TBLog("\(status!.pic_urls!.count)--\(status?.text)")
        return status!.imageURLs!.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionImageCell", forIndexPath: indexPath) as! TBPicCollectionViewCell
        cell.image = status!.imageURLs![indexPath.item].absoluteString
        return cell
    }
    
}
